'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: celery_extension.py
* @Time: 2025/11/12
* @All Rights Reserve By Brtc
'''
from celery import Task, Celery
from flask import Flask


def init_app(app:Flask):
    """celery p配置服务初始化"""
    class FlaskTask(Task):
        """定义FlaskTask 确保Celery在flask的上下文中运行, 这样可以访问flask 的配置、数据库等内容"""
        def __call__(self,*args,**kwargs):
            with app.app_context():
                return self.run(*args,**kwargs)

    #1、创建Celery应用并配置
    celery_app = Celery(app.name, task_cls=FlaskTask)
    celery_app.config_from_object(app.config["CELERY"])
    celery_app.set_default()

    #2、将celery挂载到app 的扩展中
    app.extensions["celery"] = celery_app
